#!/usr/bin/perl -w

# This script seeks to replicate the functionality of this simple script
# and apply it to other types of bgg scripts.
#!/usr/bin/zsh
#echo -n $(pbpaste | sed -e 's/[^[0-9]*\([0-9][0-9]*\).*/\[imageid=\1\]/') | pbcopy

my ($clipboard, $line, $url_found, $new_tag, @parts);

open($clipboard, "pbpaste |");
$line = <$clipboard>;
close($clipboard);

my ($baseurl) = 'https?://(?:www\.|rss\.|bgg\.|)?(?:boardgamegeek.com|geekdo.com)/';

$url_found = 1;
if (@parts = $line =~ /${baseurl}image\/([0-9]+)/) {
#  print "Found image tag\n";
  $new_tag = "[imageid=$parts[0]]";
} elsif (@parts = $line =~ /${baseurl}(?:board)?game(?:expansion)?\/([0-9]+)/) {
#  print "Found boardgame tag\n";
  $new_tag = "[gameid=$parts[0]]";
} elsif (@parts = $line =~ /${baseurl}geeklist\/([0-9]+)/) {
#  print "Found geeklist tag\n";
  $new_tag = "[geeklist=$parts[0]][/geeklist]";
} elsif (@parts = $line =~ /${baseurl}thread\/([0-9]+)/) {
  print "Found thread tag\n";
  $new_tag = "[thread=$parts[0]][/thread]";
} elsif (@parts = $line =~ /${baseurl}filepage\/([0-9]+)/) {
#  print "Found file tag\n";
  $new_tag = "[filepage=$parts[0]][/filepage]";
} elsif (@parts = $line =~ /${baseurl}article\/([0-9]+)/) {
#  print "Found article tag\n";
  $new_tag = "[article=$parts[0]][/article]";
} elsif (@parts = $line =~ /${baseurl}(?:boardgame)publisher\/([0-9]+)/) {
  $new_tag = "[publisherid=$parts[0]]";
} elsif (@parts = $line =~ /${baseurl}wiki\/page\/(.+)/) {
  $parts[0] =~ y/_/ /;
  $new_tag = "[[$parts[0]]]";
} else {
  # fall-through case. This saves us from the old behavior
  print "No URL found\n";
  $url_found = 0;
}

if ($url_found) {
#  print "New Tag $new_tag\n";

  open ($clipboard, "| pbcopy");
  print $clipboard "$new_tag";
  close ($clipboard);
}
